function [Random_FinalResult] = Random_finalsub_CL(ard_local_final,ard_pair_final,B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data)

cvx_solver mosek
cvx_begin
variables t_w t_ul(1,num_u) TT_ul(1,num_u) ...
    t_u(1,num_u) t_o(num_u,num_h) d_uh(num_u,num_h) t_hu(num_u,num_h) TT_up(1,num_u) ...
    z_o(num_u,num_h) Q_u(1,num_u) S_u(1,num_u) Q_uh(num_u,num_h) S_uh(num_u,num_h)
expressions Target Co_1(1,num_u) Co_2(1,num_h) Co_3(1,num_u) Co_4(1,num_u)
for n = 1:num_u
    if ard_local_final(n) == 1
        Target = Target + TT_ul(n);
    elseif (1-ard_local_final(n)) == 1
        Target = Target + TT_up(n);
        for m = 1:num_h
            if ard_pair_final(n,m) == 1
                Co_1(n) = Co_1(n) + (1/H_uh(n,m))*z_o(n,m) - (1/H_uh(n,m))*t_o(n,m);
                Co_3(n) = Co_3(n) + t_o(n,m) + t_hu(n,m);
                Co_4(n) = Co_4(n) + d_uh(n,m);
            end
        end
    end
end
for m = 1:num_h
    for n = 1:num_u
        if (1 - ard_local_final(n)) == 1
            if ard_pair_final(n,m) == 1
                Co_2(m) = Co_2(m) + cpu*pow_p(bit_to_cycle(n)*Data(n),3)*Q_uh(n,m);
            end
        end
    end
end
minimize Target
subject to
t_w >= 0;
for n = 1:num_u
    if ard_local_final(n) == 1
        cpu*pow_p(bit_to_cycle(n)*Data(n),3)*pow_p(t_ul(n),-2) <= eff*P_w*H_wu(n)*t_w;
        t_ul(n) >= 0;
        t_ul(n) <= TT_ul(n);
        t_w <= TT_ul(n);
        TT_ul(n) >= 0;
    elseif (1-ard_local_final(n)) == 1
        cpu*pow_p(bit_to_cycle(n)*Data(n),3)*Q_u(n) + Co_1(n) <= eff*P_w*H_wu(n)*t_w;
        t_u(n) >= 0;
        t_u(n) <= TT_up(n);
        t_w + Co_3(n) <= TT_up(n);
        TT_up(n) >= 0;
        {(1-Co_4(n)),S_u(n),t_u(n)} == rotated_lorentz(1);
        {S_u(n),Q_u(n),(1-Co_4(n))} == rotated_lorentz(1);
        Q_u(n) >= 0;
        S_u(n) >= 0;
        for m = 1:num_h
            Co_2(m) <= eff*P_w*H_wh(m)*t_w;
            if ard_pair_final(n,m) == 1
                0 <= d_uh(n,m) <= 1;
                t_o(n,m) >= 0;
                t_hu(n,m) >= 0;
                d_uh(n,m)*Data(n)*log(2)*pow_p(B,-1) + rel_entr(t_o(n,m),z_o(n,m)) <= 0;
                z_o(n,m) >= 0;
                {d_uh(n,m),S_uh(n,m),t_hu(n,m)} == rotated_lorentz(1);
                {S_uh(n,m),Q_uh(n,m),d_uh(n,m)} == rotated_lorentz(1);
                Q_uh(n,m) >= 0;
                S_uh(n,m) >= 0;
            end
        end
    end
end
cvx_end
Random_FinalResult = cvx_optval;

end